package com.bank.ui.admin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class WithdrawPanel extends JPanel {
    private JTextField cardIDField;
    private JTextField amountField;
    private JButton withdrawButton;
    private JLabel messageLabel;
    private List<WithdrawListener> withdrawListeners = new ArrayList<>();

    public WithdrawPanel() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        // 卡号输入框
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        add(new JLabel("卡号:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        cardIDField = new JTextField(16);
        add(cardIDField, gbc);

        // 金额输入框
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.EAST;
        add(new JLabel("金额:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        amountField = new JTextField(10);
        add(amountField, gbc);

        // 取款按钮
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        withdrawButton = new JButton("取款");
        add(withdrawButton, gbc);

        // 消息标签
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        messageLabel = new JLabel("", JLabel.CENTER);
        add(messageLabel, gbc);

        // 添加事件监听器
        withdrawButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                performWithdraw();
            }
        });
    }

    // 执行取款操作
    private void performWithdraw() {
        String cardID = cardIDField.getText();
        String amountStr = amountField.getText();

        // 验证输入
        if (cardID.isEmpty() || amountStr.isEmpty()) {
            showErrorMessage("卡号和金额不能为空！");
            return;
        }

        try {
            double amount = Double.parseDouble(amountStr);
            if (amount <= 0) {
                showErrorMessage("金额必须大于0！");
                return;
            }

            // 清除之前的错误消息
            messageLabel.setText("");

            // 触发取款事件
            fireWithdrawEvent(new WithdrawEvent(this, cardID, amount));

        } catch (NumberFormatException e) {
            showErrorMessage("请输入有效的金额！");
        }
    }

    // 显示错误消息
    private void showErrorMessage(String message) {
        messageLabel.setText(message);
        messageLabel.setForeground(Color.RED);
    }

    // 添加取款监听器
    public void addWithdrawListener(WithdrawListener listener) {
        withdrawListeners.add(listener);
    }

    // 移除取款监听器
    public void removeWithdrawListener(WithdrawListener listener) {
        withdrawListeners.remove(listener);
    }

    // 触发取款事件
    private void fireWithdrawEvent(WithdrawEvent event) {
        for (WithdrawListener listener : withdrawListeners) {
            listener.withdrawRequested(event);
        }
    }

    // 取款事件类
    public static class WithdrawEvent {
        private Object source;
        private String cardID;
        private double amount;

        public WithdrawEvent(Object source, String cardID, double amount) {
            this.source = source;
            this.cardID = cardID;
            this.amount = amount;
        }

        public Object getSource() {
            return source;
        }

        public String getCardID() {
            return cardID;
        }

        public double getAmount() {
            return amount;
        }
    }

    // 取款监听器接口
    public interface WithdrawListener {
        void withdrawRequested(WithdrawEvent event);
    }
}